package mogiksaop.qliphoth.common.entities.models;

import org.lwjgl.opengl.GL11;

import mogiksaop.qliphoth.client.lib.LibResources;
import mogiksaop.qliphoth.client.models.smd.ValveStudioModel;
import mogiksaop.qliphoth.client.models.smd.ValveStudioModelLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.ModelBase;
//import net.minecraft.client.model.ModelBiped;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;

public abstract class ModelSMD extends ModelBase {
	
	private ValveStudioModel Model;
	private float size,offX,offY,offZ;
	private String modelTexturePath;
	private boolean hasAnime;
	
	public ModelSMD() {
		super();
	}

	public ModelSMD(float size, float offX, float offY, float offZ, String modelTexturePath, boolean hasAnime) {
		super();
		this.size = size;
		this.offX = offX;
		this.offY = offY;
		this.offZ = offZ;
		this.modelTexturePath = modelTexturePath;
		this.hasAnime = hasAnime;
	}

	public ValveStudioModel getModel() {
		return this.Model;
	}

	public void setModel(String ModelPQCPath) {
		this.Model = (ValveStudioModel) ValveStudioModelLoader.instance.loadModel(new ResourceLocation(LibResources.PREFIX_ENTITY_MODEL +  ModelPQCPath));
	}

	@Override
	public void render(Entity entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scale) {
		Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation(LibResources.PREFIX_ENTITY_TEXTURE + this.modelTexturePath));
		standardGL();
		if(hasAnime) {
			animationConvert(entity);
		}
	}

	private void standardGL() {
		//原mod内渲染方式
//		GL11.glPushMatrix();
//		GL11.glRotatef(180f, 0f, 0f, 1f);
//		GL11.glColor4f(0f, 0f, 0f, 3f);
//		GL11.glRotatef(90f, 1f, 0f, 0f);
//		GL11.glRotatef(180f, 0f, 0f, 1f);
//		GL11.glTranslatef(0f, 0f, 10.3f);
//		GL11.glScalef(s, s, s);
//		this.Model.renderAll();
//		GL11.glPopMatrix();
		
		GL11.glPushMatrix();
		GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
		GL11.glRotatef(90.0F, 1.0F, 0.0F, 0.0F);
		GL11.glRotatef(180.0F, 0.0F, 0.0F, 1.0F);
		GL11.glTranslatef(this.offX, this.offY, this.offZ);
		GL11.glScalef(this.size, this.size, this.size);
		this.Model.renderAll();
		GL11.glPopMatrix();
	}

	private void animationConvert(Entity entity) {
		this.Model.animate();
		if (entity.prevPosX == entity.posX && entity.prevPosZ == entity.posZ) {
			this.Model.setAnimation("idle");
		} else {
			this.Model.setAnimation("walk");
		}
		this.Model.currentSequence.current().setCurrentFrame(entity.ticksExisted % this.Model.currentSequence.current().totalFrames);
	}

}
